﻿Imports System.Data.OleDb
Public Class Register1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'IF REDIRECTED FROM THE SAME PAGE/ Read what value is stored about the user and show
        If Not IsPostBack Then

            'SQL Statement
            Dim sql As String = "SELECT * FROM Profile WHERE username=@f7 "
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ArduinoPlaygroundConnectionString").ConnectionString)
            conn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@f7", User.Identity.Name)

            'Reader
            Dim dr = cmd.ExecuteReader()
            dr.Read()

            'Checks for Presense, if record already exist
            If Not IsDBNull(dr("FirstName")) Then tb_firstName.Text = dr("FirstName")
            If Not IsDBNull(dr("LastName")) Then tb_lastName.Text = dr("LastName")

            If Not IsDBNull(dr("DOB")) Then
                Dim DOB As DateTime = dr("DOB")
                ddl_YYYY.SelectedValue = DOB.Year
                ddl_MM.SelectedValue = DOB.Month
                ddl_DD.SelectedValue = DOB.Day
            End If
            If Not IsDBNull(dr("Occupation")) Then tb_Occupation.Text = dr("Occupation")
            If Not IsDBNull(dr("Country")) Then tb_Country.Text = dr("Country")

        End If

        lbl_Name.Text = User.Identity.Name
    End Sub

    Protected Sub btn_update_Click(sender As Object, e As EventArgs) Handles btn_update.Click
        'UPDATE PROFILE WITH/WITHOUT PICTURE
        Dim newFileName As String = ""
        If fu_avatar.HasFile Then
            ''Unique Name
            newFileName = Guid.NewGuid().ToString() & getSuffix(fu_avatar.FileName)
            ''Check Image Upload & assign Address
            If fu_avatar.HasFile Then
                fu_avatar.SaveAs(Server.MapPath("Pictures") & "/" & newFileName)
            End If
        End If

        'Fomatting to correct DATETIME 
        Dim DOB As DateTime = New DateTime(CInt(ddl_YYYY.SelectedValue), CInt(ddl_MM.SelectedValue), CInt(ddl_DD.SelectedValue))

        'Check if Pro Pic Present
        Dim sql As String
        If fu_avatar.HasFile Then
            sql = "UPDATE Profile SET FirstName=@f1, LastName=@f2, Avatar=@f3, DOB=@f4, Occupation=@5, Country=@6 WHERE username=@f7"
        Else
            sql = "UPDATE Profile SET FirstName=@f1, LastName=@f2, DOB=@f4, Occupation=@5, Country=@6 WHERE username=@f7"
        End If

        'SQL Statement
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ArduinoPlaygroundConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        'Value Substitution (Check if user wish to change Pro Pic)
        cmd.Parameters.AddWithValue("@f1", tb_firstName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_lastName.Text)
        If fu_avatar.HasFile Then cmd.Parameters.AddWithValue("@f3", "Pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f4", DOB.Date)
        cmd.Parameters.AddWithValue("@f5", tb_Occupation.Text)
        cmd.Parameters.AddWithValue("@f6", tb_Country.Text)
        cmd.Parameters.AddWithValue("@f7", User.Identity.Name)

        'COnnection
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()


    End Sub

    ''FUNCTION TO SPLIT FILE NAME
    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return suffix
    End Function
End Class